<template>
	<view class="page">



		<view>
			<view class="">
				<cu-custom>
					<block style="color: #777777;font-size: 48rpx;" slot="content">登录</block>
				</cu-custom>
			</view>
		</view>

		<view class="logo">
			<image style="width: 200rpx;height: 200rpx;" src="../../static/login/logo.png"></image>
		</view>

		<view>
			<view style="text-align: center;margin-top: 40rpx;color: #1890FF;">测试内置账号</view>
			<view class="flex flex-wrap" style="margin: 0 40rpx;">
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange"
					@click="goA('1')">监管部门小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange"
					@click="goA('2')">检测企业小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange"
					@click="goA('3')">维保小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange"
					@click="goA('4')">业主企业小程序</button>
			</view>

		</view>



		<view class="flex flex-direction">
			<!-- <button class="cu-btn margin-tb-sm lg my-btn" @tap="wxLogin">授权登录</button> -->
			<button class="cu-btn margin-tb-sm lg my-btn" @getuserinfo="appLoginWx"
				open-type="getUserInfo">授权登录</button>
			<text class="reg" @click="reg">用户注册</text>

		</view>

		<!-- 底部授权 -->
		<!-- 		<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white">
					<view class="action text-green">确定</view>
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
				<view class="padding-xl" style="background-image: url(../../static/home/aHome/bg_jianguan.png);">
					<button class="cu-btn margin-tb-sm lg my-btn" @getphonenumber="onGetPhoneNumber"
						open-type="getPhoneNumber">授权登录</button>
				</view>
			</view>
		</view> -->

		<!-- 弹出授权 -->
		<view class="cu-modal" :class="modalName=='DialogModal1'?'show':''">
			<view class="cu-dialog">
				<!-- 						<view class="cu-bar bg-white justify-end">
							
						</view> -->
				<view class="padding-xl">
					<view class="bind-tip">绑定手机号码</view>
					<text>请先绑定手机号码在进行操作</text>
					<button class="cu-btn margin-tb-sm lg my-btn-1" @getphonenumber="onGetPhoneNumber"
						open-type="getPhoneNumber">微信用户一键登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import WXBizDataCrypt from "@/common/WXBizDataCrypt.js"
	import * as constants from '@/common/config.js';

	export default {
		created: function() {
			// 免登录
			try {
				const value = uni.getStorageSync('token');
				console.info("缓存的角色value:" + value)
				if (value) {
					console.info("缓存的角色code:" + uni.getStorageSync('roleCode'))
					getApp().globalData.role = uni.getStorageSync('page')
					uni.reLaunch({
						url: "../index/index?id=1"
					});
				}
			} catch (e) {
				// error
			}
		},
		data() {
			return {
				code: '',
				openid: '',
				session_key: '',
				encryptedData: '"1j3LKjVEI/oP0CGjEfpaLwwrRr4S3srBLdI1fF8vZ54x3UydPCeHtWdTllNcdf8Gvx6WEwvJjyDeMAdejF2jyRKRuQpyr2M2AlOg19IDOwZcXlAlQg9Upq4N0/XmVeaMojzMGgI1s0qLc4KA5jxhIzPPrhed+CYJkny5VG9HVumuXkeWAdXB526KGXJEF2/uDjQDCaRlT/Pc4Nqu2l6YU05LhPpSoVSA3hGG6Ivi26uoHL3a89NFP/0PT7IQgaL4wndNG2D5N3Ntf9e0YYtBI7aU45KvNDvBbGVB/AlptOSdVhKhvqWHHln9SSzqVzlvtpF8zM1tc/gaV8ADY9RrNR5PVbH4XN0j1ZSuqkihGE2n0Ks1pG2GqkVI+WljwhU0AYG2PiegvP5VyFyQhyTwjKByzOUJxcw4or26KWPptJ81ivc4QuiCZqvX5jW/tXhX51LxdZYRLJTxlm1JHn67HA=="',
				iv: '"RvxfowsZ1oLxnoTAxMZ6fw=="',
				modalName: '',
				phone: '',
				role: ''

			};
		},
		methods: {
			goA(index) {
				let phone = ''
				switch (index) {
					case '1':
						phone = "18999271727"
						getApp().globalData.role = 'A'
						break;
					case '2':
						phone = "17797826546"
						getApp().globalData.role = 'B'
						break;
					case '3':
						phone = "15809024803"
						getApp().globalData.role = 'C'
						break;
					case '4':
						phone = "15518699868"
						getApp().globalData.role = 'D'
						break;
				}
				// 获取token
				this.setToken(phone)
			},
			setToken(phone) {
				let that = this
				let app = getApp()
				app.globalData.request({
					url: 'wx/auth/getByPhone',
					data: phone
				}).then(function(res) {
					
					// 登录成功，
					that.phone = phone
					console.info(">><<" + res.data.data)
					if (res.data.code == 0) {
						if (res.data.data.data.userInfo) {
							console.info("name:" + res.data.data.data.userInfo.name)
							that.role = res.data.data.data.userInfo.roleCode
							// app.globalData.setToken("roleCode", res.data.data.data.userInfo.roleCode)
							// app.globalData.setToken("mobile", res.data.data.data.userInfo.mobile)
							// app.globalData.setToken("name", res.data.data.data.userInfo.name)

							// app.globalData.setToken("uid", res.data.data.data.userInfo.ucId)
							// app.globalData.setToken("authorization", res.data.data.data.token)
							
							
							uni.setStorageSync('roleCode', res.data.data.data.userInfo.roleCode);
							uni.setStorageSync('mobile', res.data.data.data.userInfo.mobile);
							uni.setStorageSync('name', res.data.data.data.userInfo.name);
							uni.setStorageSync('uid', res.data.data.data.userInfo.ucId);
							uni.setStorageSync('authorization', res.data.data.data.token);


							// TODO 根据rolecode跳转页面。
							if (res.data.data.data.userInfo.roleCode) {

								if (res.data.data.data.userInfo.roleCode.indexOf("WB") > -1) {
									getApp().globalData.role = 'C'
									app.globalData.setToken("page", 'C')
									uni.reLaunch({
										url: "../index/index?id=1"
									});
								} else if (res.data.data.data.userInfo.roleCode.indexOf("JC") > -1) {
									getApp().globalData.role = 'B'
									app.globalData.setToken("page", 'B')
									uni.reLaunch({
										url: "../index/index?id=1"
									});
								} else if (res.data.data.data.userInfo.roleCode.indexOf("XZ") > -1) {
									getApp().globalData.role = 'D'
									app.globalData.setToken("page", 'D')
									uni.reLaunch({
										url: "../index/index?id=1"
									});
								} else if (res.data.data.data.userInfo.roleCode.indexOf("XT") > -1) {
									getApp().globalData.role = 'A'
									app.globalData.setToken("page", 'A')
									uni.reLaunch({
										url: "../index/index?id=1"
									});
								} else {
									uni.showToast({
										title: "无权限！",
										icon: 'error'
									})
								}

							} else {
								uni.showModal({
									title: "无权限",
									showCancel: false
								})
							}
						} else {
							uni.showToast({
								title: "登录失败！",
								icon: 'error'
							})
						}
					}
				}).catch(function(e) {
					uni.showModal({
						title: "用户无权限进行登录",
						showCancel: false
					})
				})
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					console.log("用户点击了接受")
					console.log(e.detail);
					let encryptedData = e.detail.encryptedData
					let errMsg = e.detail.errMsg
					let iv = e.detail.iv

					let pc = new WXBizDataCrypt(constants.APPID, this.session_key);
					let data = pc.decryptData(encryptedData, iv);
					console.log(data) //data就是最终解密的用户信息  

					this.setToken(data.purePhoneNumber)

				} else {
					console.log(e)
					console.log("用户点击了拒绝")
				}

				this.modalName = ''
			},
			appLoginWx() {
				// 获取用户信息
				uni.getUserInfo({
					provider: 'weixin',
					lang: 'zh_CN',
					success: userInfo => {
						this.encryptedData = userInfo.encryptedData
						this.iv = userInfo.iv
						console.log(userInfo, 'userInfo');
						uni.login({
							provider: 'weixin',
							success: loginInfo => {
								console.log(loginInfo, 'loginInfo');
								this.wxLogin()
							}
						});
						this.modalName = 'DialogModal1'
					},
					fail: err => {
						console.log(err, 'err')
					}
				});
			},
			reg() {
				uni.navigateTo({
					url: '../reg/reg'
				});
			},
			getUserInfo(res) {
				console.info("res:" + res)
			},
			wxLogin() {
				let self = this
				uni.login({
					provider: 'weixin',
					success: function(code_res) {
						if (code_res.errMsg == "login:ok") {
							let code = code_res.code;
							console.info("///", code)

							let app = getApp()
							app.globalData.request({
								url: 'wx/auth/login_by_weixin',
								data: code
							}).then(function(res) {
								self.openid = res.data.data.data.openId
								self.session_key = res.data.data.data.sessionKey
							})
						}
					}
				})
			},
		}
	}
</script>

<style scoped>
	.page {
		height: 100vh;
		background-color: #F5F6F8;
	}

	.logo {
		width: 200rpx;
		height: 200rpx;
		margin: 0 auto;
		margin-top: 100rpx;
		background: #D8D8D8;
		border-radius: 16rpx;
		border-radius: 16rpx;
	}

	.my-btn {
		height: 94rpx;
		margin-top: 80rpx;
		background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
		box-shadow: 0 0 12rpx 0 rgba(250, 181, 58, 0.50);
		border-radius: 24rpx;
		ont-family: PingFangSC-Medium;
		color: #FFFFFF;
		letter-spacing: 0;
		text-align: center;
	}

	.my-btn-1 {
		height: 94rpx;
		background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
		box-shadow: 0 0 12rpx 0 rgba(250, 181, 58, 0.50);
		border-radius: 24rpx;
		ont-family: PingFangSC-Medium;
		color: #FFFFFF;
		letter-spacing: 0;
		text-align: center;
		margin-bottom: 0;
	}

	.bind-tip {
		font-size: 38rpx;
		margin-bottom: 24rpx;
	}

	.reg {
		font-family: PingFangSC-Medium;
		font-size: 24rpx;
		color: #458DF5;
		letter-spacing: 0;
		text-align: center;
	}
</style>
